package top.jiangqiang.qianyi.common.auth;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.Delegate;
import org.jetbrains.annotations.NotNull;
import top.jiangqiang.qianyi.common.auth.model.UserDetails;

import java.util.Objects;

/**
 * @Date 2024/6/4 上午10:14
 * @Author Jiangqiang
 */
public class SessionHolder {
    private static final ThreadLocal<UserDetails> SESSION_THREAD_LOCAL = new TransmittableThreadLocal<>();

    @NotNull
    public static UserDetails currentUser() {
        UserDetails userDetails = SESSION_THREAD_LOCAL.get();
        return Objects.requireNonNullElseGet(userDetails, UserDetails::new);
    }

    public static void remove() {
        SESSION_THREAD_LOCAL.remove();
    }

    public static <T extends UserDetails> void setUser(T user) {
        SESSION_THREAD_LOCAL.set(user);
    }
}
